﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using BHXH_NT.Properties;
using System.Data.SqlClient;
namespace BHXH_NT
{
    public partial class frmSetting : Form
    {
        public frmSetting()
        {
            InitializeComponent();
        }

        private void frmSetting_Load(object sender, EventArgs e)
        {
            flxSetting.Rows[1][1] = "NHATRANG";
            flxSetting.Rows[2][1] = "CAMRANH";
            flxSetting.Rows[3][1] = "CAMLAM";
            flxSetting.Rows[4][1] = "NINHHOA";
            flxSetting.Rows[5][1] = "VANNINH";
            flxSetting.Rows[6][1] = "DIENKHANH";
            flxSetting.Rows[7][1] = "KHANHVINH";
            flxSetting.Rows[8][1] = "KHANHSON";
            flxSetting.Rows[9][1] = "TINH";
            flxSetting.Rows[10][1] = "USER";
            flxSetting.Rows[11][1] = "PASS";
                       
            flxSetting.Rows[1][2] = global::BHXH_NT.Properties.Settings.Default.MISBHXH_NHATRANG_CONNECTIONTRING;
            flxSetting.Rows[2][2] = global::BHXH_NT.Properties.Settings.Default.MISBHXH_CAMRANH_CONNECTIONTRING;
            flxSetting.Rows[3][2] = global::BHXH_NT.Properties.Settings.Default.MISBHXH_CAMLAM_CONNECTIONTRING;
            flxSetting.Rows[4][2] = global::BHXH_NT.Properties.Settings.Default.MISBHXH_NINHHOA_CONNECTIONTRING;
            flxSetting.Rows[5][2] = global::BHXH_NT.Properties.Settings.Default.MISBHXH_VANNINH_CONNECTIONTRING;
            flxSetting.Rows[6][2] = global::BHXH_NT.Properties.Settings.Default.MISBHXH_DIENKHANH_CONNECTIONTRING;
            flxSetting.Rows[7][2] = global::BHXH_NT.Properties.Settings.Default.MISBHXH_KHANHVINH_CONNECTIONTRING;
            flxSetting.Rows[8][2] = global::BHXH_NT.Properties.Settings.Default.MISBHXH_KHANHSON_CONNECTIONTRING;
            flxSetting.Rows[9][2] = global::BHXH_NT.Properties.Settings.Default.MISBHXH_TINH_CONNECTIONTRING;
            flxSetting.Rows[10][2] = global::BHXH_NT.Properties.Settings.Default.User;
            flxSetting.Rows[11][2] = global::BHXH_NT.Properties.Settings.Default.Pass;

            
            flxSetting.Rows[1][3] = global::BHXH_NT.Properties.Settings.Default.NHATRANG_TIMER;
            flxSetting.Rows[2][3] = global::BHXH_NT.Properties.Settings.Default.CAMRANH_TIMER;
            flxSetting.Rows[3][3] = global::BHXH_NT.Properties.Settings.Default.CAMLAM_TIMER;
            flxSetting.Rows[4][3] = global::BHXH_NT.Properties.Settings.Default.NINHHOA_TIMER;
            flxSetting.Rows[5][3] = global::BHXH_NT.Properties.Settings.Default.VANNINH_TIMER;
            flxSetting.Rows[6][3] = global::BHXH_NT.Properties.Settings.Default.DIENKHANH_TIMER;
            flxSetting.Rows[7][3] = global::BHXH_NT.Properties.Settings.Default.KHANHVINH_TIMER;
            flxSetting.Rows[8][3] = global::BHXH_NT.Properties.Settings.Default.KHANHSON_TIMER;
            flxSetting.Rows[9][3] = global::BHXH_NT.Properties.Settings.Default.TINH_TIMER;
            
         

         }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {

            global::BHXH_NT.Properties.Settings.Default.MISBHXH_NHATRANG_CONNECTIONTRING = flxSetting.Rows[1][2].ToString();
            global::BHXH_NT.Properties.Settings.Default.MISBHXH_CAMRANH_CONNECTIONTRING = flxSetting.Rows[2][2].ToString();
            global::BHXH_NT.Properties.Settings.Default.MISBHXH_CAMLAM_CONNECTIONTRING = flxSetting.Rows[3][2].ToString();
            global::BHXH_NT.Properties.Settings.Default.MISBHXH_NINHHOA_CONNECTIONTRING = flxSetting.Rows[4][2].ToString();
            global::BHXH_NT.Properties.Settings.Default.MISBHXH_VANNINH_CONNECTIONTRING = flxSetting.Rows[5][2].ToString();
            global::BHXH_NT.Properties.Settings.Default.MISBHXH_DIENKHANH_CONNECTIONTRING = flxSetting.Rows[6][2].ToString();
            global::BHXH_NT.Properties.Settings.Default.MISBHXH_KHANHVINH_CONNECTIONTRING = flxSetting.Rows[7][2].ToString();
            global::BHXH_NT.Properties.Settings.Default.MISBHXH_KHANHSON_CONNECTIONTRING = flxSetting.Rows[8][2].ToString();
            global::BHXH_NT.Properties.Settings.Default.MISBHXH_TINH_CONNECTIONTRING = flxSetting.Rows[9][2].ToString();

            BHXH_NT.Properties.Settings.Default.NHATRANG_TIMER = flxSetting.Rows[1][3].ToString();
            BHXH_NT.Properties.Settings.Default.CAMRANH_TIMER =  flxSetting.Rows[2][3].ToString();
            BHXH_NT.Properties.Settings.Default.CAMLAM_TIMER =   flxSetting.Rows[3][3].ToString();
            BHXH_NT.Properties.Settings.Default.NINHHOA_TIMER  = flxSetting.Rows[4][3].ToString();
            BHXH_NT.Properties.Settings.Default.VANNINH_TIMER  = flxSetting.Rows[5][3].ToString();
            BHXH_NT.Properties.Settings.Default.DIENKHANH_TIMER= flxSetting.Rows[6][3].ToString();
            BHXH_NT.Properties.Settings.Default.KHANHVINH_TIMER= flxSetting.Rows[7][3].ToString();
            BHXH_NT.Properties.Settings.Default.KHANHSON_TIMER = flxSetting.Rows[8][3].ToString();
            BHXH_NT.Properties.Settings.Default.TINH_TIMER = flxSetting.Rows[9][3].ToString();

            global::BHXH_NT.Properties.Settings.Default.User = flxSetting.Rows[10][2].ToString();
            global::BHXH_NT.Properties.Settings.Default.Pass = flxSetting.Rows[11][2].ToString();
                        

            global::BHXH_NT.Properties.Settings.Default.Save();
            this.Close();
        }

        private void btnTest_Click(object sender, EventArgs e)
        {
            if (TestConnection(BHXH_NT.Properties.Settings.Default.MISBHXH_NHATRANG_CONNECTIONTRING))
            {
                flxSetting.Rows[1][4] = "Connect thành công";
            }
            if (TestConnection(BHXH_NT.Properties.Settings.Default.MISBHXH_CAMRANH_CONNECTIONTRING))
            {
                flxSetting.Rows[2][4] = "Connect thành công";
            }
            if (TestConnection(BHXH_NT.Properties.Settings.Default.MISBHXH_CAMLAM_CONNECTIONTRING))
            {
                flxSetting.Rows[3][4] = "Connect thành công";
            }
            if (TestConnection(BHXH_NT.Properties.Settings.Default.MISBHXH_NINHHOA_CONNECTIONTRING))
            {
                flxSetting.Rows[4][4] = "Connect thành công";
            }
            if (TestConnection(BHXH_NT.Properties.Settings.Default.MISBHXH_VANNINH_CONNECTIONTRING))
            {
                flxSetting.Rows[5][4] = "Connect thành công";
            }
            if (TestConnection(BHXH_NT.Properties.Settings.Default.MISBHXH_DIENKHANH_CONNECTIONTRING))
            {
                flxSetting.Rows[6][4] = "Connect thành công";
            }
            if (TestConnection(BHXH_NT.Properties.Settings.Default.MISBHXH_KHANHVINH_CONNECTIONTRING))
            {
                flxSetting.Rows[7][4] = "Connect thành công";
            }
            if (TestConnection(BHXH_NT.Properties.Settings.Default.MISBHXH_KHANHSON_CONNECTIONTRING))
            {
                flxSetting.Rows[8][4] = "Connect thành công";
            }
            if (TestConnection(BHXH_NT.Properties.Settings.Default.MISBHXH_TINH_CONNECTIONTRING))
            {
                flxSetting.Rows[9][4] = "Connect thành công";
            }
        }
        private bool TestConnection(string sqlConnection)
        {
            try
            {

                using (SqlConnection conn = new SqlConnection(sqlConnection))
                {
                    conn.Open();
                    conn.Close();

                }
            }
            catch
            {
                return false;
            }

            return true;
        }
        private Boolean TestConnect()
        {
            //test connection1
            try
            {

                using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.MISBHXH_NHATRANG_CONNECTIONTRING))
                {
                    conn.Open();
                    conn.Close();
                    flxSetting.Rows[1][4] = "Connect thành công";
                }
                using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.MISBHXH_CAMRANH_CONNECTIONTRING))
                {
                    conn.Open();
                    conn.Close();
                    flxSetting.Rows[2][4] = "Connect thành công";
                }
                using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.MISBHXH_CAMLAM_CONNECTIONTRING))
                {
                    conn.Open();
                    conn.Close();
                    flxSetting.Rows[3][4] = "Connect thành công";
                }
                using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.MISBHXH_NINHHOA_CONNECTIONTRING))
                {
                    conn.Open();
                    conn.Close();
                    flxSetting.Rows[4][4] = "Connect thành công";
                }
                using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.MISBHXH_VANNINH_CONNECTIONTRING))
                {
                    conn.Open();
                    conn.Close();
                    flxSetting.Rows[5][4] = "Connect thành công";
                }
                using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.MISBHXH_DIENKHANH_CONNECTIONTRING))
                {
                    conn.Open();
                    conn.Close();
                    flxSetting.Rows[6][4] = "Connect thành công";
                }
                using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.MISBHXH_KHANHVINH_CONNECTIONTRING))
                {
                    conn.Open();
                    conn.Close();
                    flxSetting.Rows[7][4] = "Connect thành công";
                }
                using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.MISBHXH_KHANHSON_CONNECTIONTRING))
                {
                    conn.Open();
                    conn.Close();
                    flxSetting.Rows[8][4] = "Connect thành công";
                }
                using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.MISBHXH_TINH_CONNECTIONTRING))
                {
                    conn.Open();
                    conn.Close();
                    flxSetting.Rows[9][4] = "Connect thành công";
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Invalid Connection String");
                return false;
            }
            return true;
        }
    }
}
